package com.zzzyh.springteans.controller;

import com.zzzyh.springteans.pojo.UserInfo;
import com.zzzyh.springteans.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

//    @Autowired
//    private DataSourceTransactionManager dataSourceTransactionManager;
//
//    @Autowired
//    private TransactionDefinition transactionDefinition;

    @RequestMapping("/insert")
    @Transactional
    public boolean insertUser(UserInfo userInfo)
    {
//        TransactionStatus transaction = dataSourceTransactionManager.getTransaction(transactionDefinition);

        boolean b = userService.insertUser(userInfo) > 0;

//        dataSourceTransactionManager.commit(transaction);
//        dataSourceTransactionManager.rollback(transaction);
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return b;
    }
}
